package com.lnlyj.media.util;

import android.app.Activity;
import android.content.pm.PackageManager;

import java.util.ArrayList;
import java.util.List;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

/**
 * Created by Administrator on 2018/4/18.
 */

public class PermissionUtils {

    public final static int REQUEST_PERMISSION = 0x34;

    public interface PermissionListener {

        void onShouldShowRequestPermissions(List<String> permissions);

        //授权成功
        void onGranted();

        //授权部分
        void onGranted(List<String> grantedPermission);

        //拒绝授权
        void onDenied(List<String> deniedPermission);
    }

    /**
     * 请求单个权限
     *
     * @param activity   上下文
     * @param permission 权限
     * @return
     */
    public static void requestPermission(Activity activity, String permission, PermissionListener listener) {
        requestPermissions(activity, new String[]{permission}, listener);
    }

    /**
     * 请求多个权限
     *
     * @param activity    上下文
     * @param permissions 权限列表
     * @return
     */
    public static void requestPermissions(Activity activity, String[] permissions, PermissionListener listener) {

        //用于存放为授权的权限
        List<String> permissionList = new ArrayList<>();
        //遍历传递过来的权限集合
        for (String permission : permissions) {
            //判断是否已经授权
            if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                //未授权，则加入待授权的权限集合中
                permissionList.add(permission);
            }
        }

        if (permissionList.size() == 0) {
            listener.onGranted();
        } else {

            List<String> shouldShowPermissions = new ArrayList<>();
            for (String permission : permissionList) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
                    shouldShowPermissions.add(permission);
                }
            }

            if (shouldShowPermissions.size() > 0) {
                listener.onShouldShowRequestPermissions(shouldShowPermissions);
            }

            ActivityCompat.requestPermissions(activity, permissionList.toArray(new String[permissionList.size()]), REQUEST_PERMISSION);
        }
    }

    /**
     * 权限结果回调在onRequestPermissionsResult中调用
     *
     * @param requestCode  请求码
     * @param permissions  权限
     * @param grantResults 申请结果
     * @param listener     回调监听
     */
    public static void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults, PermissionListener listener) {
        switch (requestCode) {
            case REQUEST_PERMISSION:
                if (grantResults.length > 0) {
                    //被用户拒绝的权限集合
                    List<String> deniedPermissions = new ArrayList<>();
                    //用户通过的权限集合
                    List<String> grantedPermissions = new ArrayList<>();
                    for (int i = 0; i < grantResults.length; i++) {
                        //获取授权结果，这是一个int类型的值
                        int grantResult = grantResults[i];
                        String permission = permissions[i];

                        if (grantResult != PackageManager.PERMISSION_GRANTED) { //用户拒绝授权的权限
                            deniedPermissions.add(permission);
                        } else {  //用户同意的权限
                            grantedPermissions.add(permission);
                        }
                    }

                    if (deniedPermissions.isEmpty()) {  //用户拒绝权限为空
                        listener.onGranted();
                    } else {  //不为空
                        //回调授权失败的接口
                        listener.onDenied(deniedPermissions);
                        //回调授权成功的接口
                        listener.onGranted(grantedPermissions);
                    }
                }
                break;
            default:
                break;
        }
    }
}
